angular.module(moduleName + '.directives')
.directive('hrFilterPicker', function($timeout,$rootScope,$window) {
    return {
        restrict: 'E', //element标签方式显示
        templateUrl: "hr-filter-picker/hr-filter-picker.html", //设置指令对应的html代码
        require: "^hrFilterPickerBar",
        replace: true,
        scope: {
            click: '&',
            data: '=',
            ngModel: '=',
            text: '@',
            value: '@',
            type:'@',
            defaultText:'@'
        },
        link: function(scope, iElm, iAttrs, filterPickerBarCtrl) {

            scope.isSingle = true;
            if(scope.type == 'mutiple'){
                scope.isSingle = false;
            }

            scope.pickerStyle = {"maxHeight":$window.innerHeight*0.7+'px'};
            scope.isActive = false;

            //筛选控件的点击
            scope.picker = function() {
                if(scope.isActive){
                    scope.isActive= false;
                    filterPickerBarCtrl.closeBackdrop();
                }
                else{
                    $rootScope.$emit('closeShowBackdrop');
                    scope.isActive = true;
                    filterPickerBarCtrl.showBackdrop();
                }
            }
            //多选控件的确认按钮
            scope.confirm = function(){

                var results = [];
                scope.dataSource.forEach(function(data) {
                    if(data.checked){
                        results.push(data);
                    }
                });
                scope.ngModel = results;

                if(results.length>1){
                    scope.selectedData = {
                        text:'已选择"'+results[0].text+'"等部门',
                        value:""
                    };
                }
                else if(results.length==1){
                    scope.selectedData = {
                        text:results[0].text,
                        value:""
                    };
                }
                else{
                    scope.selectedData = {
                        text:scope.defaultText,
                        value:""
                    };
                }

                $timeout(function() {
                    scope.isActive= false;
                    filterPickerBarCtrl.closeBackdrop();
                    scope.click();
                }, 1)

            };
            //单选控件的点击
            scope.onclick = function(item) {
                scope.dataSource.forEach(function(data) {
                    data.checked = false;
                });
                item.checked = true;
                scope.selectedData = item;
                scope.ngModel = item.value;
                $timeout(function() {
                    filterPickerBarCtrl.closeBackdrop();
                    scope.isActive = false;
                    scope.click();
                }, 1)
            }
            //多选控件的重置功能
            scope.reset = function(){
                scope.dataSource.forEach(function(data) {
                    data.checked = false;
                });
            }
            //多选控件的选项的点击事件
            scope.enter = function(item){
                item.checked = !item.checked;
            }

            $rootScope.$on('closeShowBackdrop',function(value){
                scope.isActive = false;
            });
            scope.stopPropagation = function($event){
                $event.stopPropagation();
            };

            scope.$watch('data', function() {
                //debugger;
                scope.dataSource = [];
                if (scope.data && scope.data.length > 0) {
                    scope.data.forEach(function(data) {
                        if (scope.text) {
                            scope.dataSource.push({
                                text: data[scope.text],
                                value: data[scope.value],
                                checked: false
                            })
                        } else {
                            scope.dataSource.push({
                                text: data,
                                value: data,
                                checked: false
                            })
                        }
                    });

                }
                if(scope.defaultText){
                    scope.selectedData = {
                        text:scope.defaultText,
                        value:""
                    };
                }
                else{
                    scope.selectedData = scope.dataSource[0];
                    if (scope.dataSource[0]) {
                        scope.dataSource[0].checked = true;
                    }
                }


            })

        }
    };
});
